<?php

App::uses('AppController', 'Controller');

/**
 * Resources Controller
 *
 * @property Resource $Resource
 */
class ResourcesController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Resource->locale = $this->Session->read('Language.code');
        $this->Resource->recursive = 0;
        $search = $this->request->data;
        if ($search != null) {
            $paginate = $this->paginate('Resource', array(
                'Resource.res_name LIKE' => '%' . $search['Resources']['search'] . '%'
                    )
            );
        } else {
            $paginate = $this->paginate();
        }
        $this->set('resources',$paginate);
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->Resource->locale = $this->Session->read('Language.code');
        if (!$this->Resource->exists($id)) {
            throw new NotFoundException('Invalid resource', 'default', array('class' => 'alert alert-danger'));
        }
        $options = array('conditions' => array('Resource.' . $this->Resource->primaryKey => $id));
        $this->set('resource', $this->Resource->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        $this->Resource->locale = $this->Session->read('Language.code');
        $this->loadModel('Language');
        if ($this->request->is('post')) {
            $this->Resource->locale = $this->request->data['Resources']['Language'];
            $this->Resource->create();
            if ($this->Resource->save($this->request->data)) {
                $this->Session->setFlash('The new resource has been saved', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {

                $this->Session->setFlash('The new resource could not be saved. Please, try again.','default',array('class'=>'alert alert-danger'));

            }
        }
        $this->Resource->Catagory->locale = $this->Session->read('Language.code');
        $cats = $this->Resource->Catagory->find('list');
        $this->set(compact('cats'));
        $questionnaire = $this->Resource->Questionnaire->find('list');
        $this->set(compact('questionnaire'));
        $language = $this->Language->find('list', array('fields' => array('Language.code', 'Language.name')));
        $this->set(compact('language'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->loadModel('Language');
        $this->Resource->locale = $this->Session->read('Language.code');
        if (!$this->Resource->exists($id)) {
            throw new NotFoundException('Invalid resource', 'default', array('class' => 'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $this->Resource->locale = $this->request->data['Resources']['Language'];
            if ($this->Resource->save($this->request->data)) {
                $this->Session->setFlash('The resource has been successfully edited', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The resource could not be edited. Please, try again.','default',array('class'=>'alert alert-danger'));

            }
        } else {
            $options = array('conditions' => array('Resource.res_id' => $id));
            $this->request->data = $this->Resource->find('first', $options);
            $this->Resource->Catagory->locale = $this->Session->read('Language.code');
            $cats = $this->Resource->Catagory->find('list');
            $this->set(compact('cats'));
            $questionnaire = $this->Resource->Questionnaire->find('list');
            $this->set(compact('questionnaire'));
            $language = $this->Language->find('list', array('fields' => array('Language.code', 'Language.name')));
            $this->set(compact('language'));
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Resource->id = $id;
        if (!$this->Resource->exists()) {
            throw new NotFoundException('Invalid resource', 'default', array('class' => 'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Resource->delete()) {
            $this->Session->setFlash('Resource deleted', 'default', array('class' => 'alert alert-info'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Resource was not deleted', 'default', array('class' => 'alert alert-danger'));
        $this->redirect(array('action' => 'index'));
    }

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index', 'view', 'show');
    }

    /**
     * display A-Z link 
     * @param type $str
     */
    public function show($filter = null) {
        $this->Resource->locale = $this->Session->read('Language.code');


        // query all distinct first letters used in names
        $letters = $this->Resource->query('SELECT DISTINCT SUBSTRING(`res_name`, 1, 1) FROM `resources` ORDER BY `res_name`');

        $links = array();
        // push all letters into a non-nested array
        foreach ($letters as $row) {
            array_push($links, current($row[0]));
        }

        $this->set('links', $links);

        if ($filter != 'all') {
            $data = $this->Resource->find('all', array('conditions' => array('Resource.res_name LIKE' => $filter . '%')));
        } else if ($filter == 'all') {
            $data = $this->paginate('Resource');
        }

        $search = $this->request->data;
        if ($search != null) {
            $data = $this->paginate('Resource', array(
                'Resource.res_name LIKE' => '%' . $search['Resources']['search'] . '%'
                    )
            );
        }

        $this->set('data', $data);
        $this->set('filter', $filter);
    }

}
